package com.cjc.mylibrary.fragment;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.widget.Toast;

import com.cjc.mylibrary.R;
import com.cjc.mylibrary.menu.lib.SlidingMenu;
import com.cjc.mylibrary.tools.Global;
import com.cjc.mylibrary.zip.tools.CompresszZipFile;

/**
 * 
 ***************************************************** 
 * <hr>
 * <dt><span class="strong">类功能简介:</span></dt>
 * <dd>菜单框架初始化页面</dd>
 * <dt><span class="strong">创建时间:</span></dt>
 * <dd>2014-8-21 下午2:55:48</dd>
 * <dt><span class="strong">公司:</span></dt>
 * <dd>菜单框架初始化页面</dd>
 * 
 * @author aa1000777 - Email:aa1000777@qq.com
 ***************************************************** 
 */
public class MainActivity extends FragmentActivity {
	private SlidingMenu menu;// 菜单
	private Context context;// 上下文对象
	private long exitTime = 0;// 按两次后退的记时
	public static boolean isForeground = false;
	public final String MenuName = "Activity/Theory";// 设置初始页面

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.slidingmenu_main);
		/* 初始化数据 */
		initData();
		context = this;
		int i = getResources().getDisplayMetrics().widthPixels;
		menu = new SlidingMenu(context);
		menu.setBehindWidth(i / 10 * 7);// 设置SlidingMenu菜单的宽度
		menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		menu.setShadowWidthRes(R.dimen.slidingmenu_shadow_width);
		menu.setShadowDrawable(R.drawable.slidingmenu_shadow);
		// menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		menu.setFadeDegree(0.35f);
		menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
		menu.setContent(R.layout.slidingmenu_content);
		menu.setMenu(R.layout.slidingmenu_menu);
		menu.setMode(SlidingMenu.LEFT);
		Global.menu = menu;
		// 要跳转的页面
		ListDataFragment fragment = new ListDataFragment();
		Bundle args = new Bundle();// 参数
		args.putString("MenuName", MenuName);
		fragment.setArguments(args);
		getSupportFragmentManager().beginTransaction().replace(R.id.slidingmenu_content, fragment).commit();
		getSupportFragmentManager().beginTransaction().replace(R.id.slidingmenu_menu, new MainLeftFragment()).commit();
	}

	/**
	 * 
	 ***************************************************** 
	 * 方法简介: 初始化数据
	 ***************************************************** 
	 */
	private void initData() {
		// 获取SD卡目录
		File f = Environment.getExternalStorageDirectory();
		// 读sdcard文件
		File file = new File(f, "MyLibrary/");
		System.out.println("看下文件路径：" + file.getPath());
		if (!file.exists()) {
			try {
				file.mkdir();
				String zipFileName = file.getPath() + "/MyLibrary.zip";
				/* 将Assets中的zip文件copy到SD中 */
				copyBigDataToSD(zipFileName);
				CompresszZipFile zip = new CompresszZipFile();
				// 解压zip文件
				zip.ReadZip(zipFileName, file.getPath());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			if (menu.isMenuShowing()) {
				menu.toggle();
				return false;
			} else if (Global.ExitToHomeFlag) {
				FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
				ft.setCustomAnimations(R.anim.push_left_in, R.anim.push_left_out, R.anim.push_right_in, R.anim.push_right_out);
				Global.ExitToHomeFlag = false;
				break;
			} else if (Global.UpdateFlag) {
				this.moveTaskToBack(true);
				return false;
			} else if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
				boolean boolflag = getSupportFragmentManager().popBackStackImmediate();
				if (!boolflag) {
					if ((System.currentTimeMillis() - exitTime) > 2000) {
						Toast.makeText(getApplicationContext(), "Click the exit program again", Toast.LENGTH_SHORT).show();
						exitTime = System.currentTimeMillis();
					} else {
						finish();
						System.exit(0);
					}
				}
				return true;
			}
			break;

		case KeyEvent.KEYCODE_MENU:
			if (!Global.UpdateFlag) {
				menu.toggle();
			} else {
				return false;
			}
			break;
		}
		return super.onKeyDown(keyCode, event);
	}

	/**
	 * 
	 ***************************************************** 
	 * 方法简介: 将Assets中的zip文件copy到SD中
	 * 
	 * @param strOutFileName
	 *            zip文件的完整路径
	 * @throws IOException
	 ***************************************************** 
	 */
	private void copyBigDataToSD(String strOutFileName) throws IOException {
		InputStream myInput;
		OutputStream myOutput = new FileOutputStream(strOutFileName);
		myInput = this.getAssets().open("MyLibrary.zip");
		byte[] buffer = new byte[1024];
		int length = myInput.read(buffer);
		while (length > 0) {
			myOutput.write(buffer, 0, length);
			length = myInput.read(buffer);
		}
		myOutput.flush();
		myInput.close();
		myOutput.close();
	}

}
